﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using Yipusoft.SoldierExam.Model;
using Yipusoft.SoldierExam.BLL;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;


public partial class Aptitude_AptitudeScore : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            #region 权限
            bool isPermission = false;
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300300"))
            {
                isPermission = true;
            }
            else
            {

                if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300307"))
                {
                    isPermission = true;
                }


            }
            if (!isPermission)
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
            #endregion
            init();
        }
    }

    private void init()
    {
        string SoldierID = Request.QueryString["SoldierID"];
        string RangerID = Request.QueryString["RangerID"];
        string ExamId = Request.QueryString["ExamID"];

        //士兵信息
        Soldier soldier = new Soldier();
        SoldierArchiveInfo info = soldier.GetSoldierInfoByID(SoldierID);
        lblDepartment.Text = info.DepartmentName;
        lblSoldierName.Text = info.SoldierName;

        //等级信息
        lblNote.Text = AptitudeRanger.GetRangerById(int.Parse(RangerID)).Note;

        dlScore.DataSource = AptitudeItem.GetListBySkill("ZZKH010100");
        dlScore.DataBind();

        dlStand.DataSource = AptitudeItem.GetListBySkill("ZZKH010200");
        dlStand.DataBind();

        AptitudeExamScoreInfo examScore = AptitudeExamScore.GetAptitudeExamScore(SoldierID, Convert.ToInt32(RangerID), Convert.ToInt32(ExamId), 13);
        if (examScore != null)
        {
            ExamScore.Text = examScore.Score.ToString();
            ExamLeader.Text = examScore.ExamLeader;
            ExamMember.Text = examScore.ExamMember;
            ExamDate.Text = examScore.ExamDate.ToString("yyyy-MM-dd");
        }
    }
    //更新
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string SoldierID = Request.QueryString["SoldierID"];
        string RangerID = Request.QueryString["RangerID"];
        string ExamId = Request.QueryString["ExamID"];
        foreach (DataListItem item in dlScore.Items)
        {
            var itemInfo = item.DataItem as AptitudeItemInfo;
            TextBox result = item.FindControl("Result") as TextBox;
            TextBox score = item.FindControl("Score") as TextBox;
            HiddenField itemID = item.FindControl("ItemID") as HiddenField;
            AptitudeExamScoreInfo examScore = AptitudeExamScore.GetAptitudeExamScore(SoldierID, Convert.ToInt32(RangerID), Convert.ToInt32(ExamId), Convert.ToInt32(itemID.Value));
            if (examScore != null)
            {
                examScore.ExamLeader = ExamLeader.Text;
                examScore.ExamMember = ExamMember.Text;
                examScore.InputDate = DateTime.Today;
                examScore.InputUser = User.Identity.Name;
                examScore.Result = result.Text;
                examScore.Score = score.Text == "" ? 0.0m : Convert.ToDecimal(score.Text);
                examScore.Standard = 0;
                AptitudeExamScore.Update(examScore);
                EventLogInfo log = new EventLogInfo();
                log.EventTime = DateTime.Now;
                log.EventType = 1;
                log.EventData = "修改一个体能考核成绩";
                log.ItemID = examScore.ScoreID.ToString();
                log.ItemName = "Combat";
                log.ItemFullUrl = "Aptitude/UpdateAptitudeScore.aspx";
                log.CreatedBy = Context.User.Identity.Name;
                log.CreatedTime = DateTime.Now;
                log.UserIP = Request.UserHostAddress;
                EventLog.Insert(log);
            }
            else
            {
                examScore = new AptitudeExamScoreInfo();
                examScore.ExamDate = ExamDate.Text == "" ? DateTime.Today : Convert.ToDateTime(ExamDate.Text);
                examScore.ExamID = int.Parse(Request["ExamID"]);
                examScore.ExamLeader = ExamLeader.Text;
                examScore.ExamMember = ExamMember.Text;
                examScore.InputDate = DateTime.Today;
                examScore.InputUser = User.Identity.Name;
                examScore.ItemID = int.Parse(itemID.Value);
                examScore.RangerID = int.Parse(Request["RangerID"]);
                examScore.Result = result.Text;
                examScore.Score = score.Text == "" ? 0.0m : Convert.ToDecimal(score.Text);
                examScore.SoldierID =new Guid(SoldierID);
                examScore.Standard = 0;
                AptitudeExamScore.Insert(examScore);
                EventLogInfo log = new EventLogInfo();
                log.EventTime = DateTime.Now;
                log.EventType = 1;
                log.EventData = "添加一个体能考核成绩";
                log.ItemID = examScore.ScoreID.ToString();
                log.ItemName = "Combat";
                log.ItemFullUrl = "Aptitude/UpdateAptitudeScore.aspx";
                log.CreatedBy = Context.User.Identity.Name;
                log.CreatedTime = DateTime.Now;
                log.UserIP = Request.UserHostAddress;
                EventLog.Insert(log);
            }
        }

        foreach (DataListItem item in dlStand.Items)
        {

            TextBox result = item.FindControl("Result") as TextBox;
            DropDownList stanard = item.FindControl("ddlStand") as DropDownList;
            HiddenField itemID = item.FindControl("ItemID") as HiddenField;
            AptitudeExamScoreInfo examScore = AptitudeExamScore.GetAptitudeExamScore(SoldierID, Convert.ToInt32(RangerID), Convert.ToInt32(ExamId), Convert.ToInt32(itemID.Value));
            if (examScore != null)
            {
                
                examScore.ExamLeader = ExamLeader.Text;
                examScore.ExamMember = ExamMember.Text;
                examScore.InputDate = DateTime.Today;
                examScore.InputUser = User.Identity.Name;
                examScore.Result = result.Text;
                examScore.Standard = int.Parse(stanard.SelectedValue);
                AptitudeExamScore.Update(examScore);
                EventLogInfo log = new EventLogInfo();
                log.EventTime = DateTime.Now;
                log.EventType = 1;
                log.EventData = "修改一个技能考核成绩";
                log.ItemID = examScore.ScoreID.ToString();
                log.ItemName = "Combat";
                log.ItemFullUrl = "Aptitude/UpdateAptitudeScore.aspx";
                log.CreatedBy = Context.User.Identity.Name;
                log.CreatedTime = DateTime.Now;
                log.UserIP = Request.UserHostAddress;
                EventLog.Insert(log);
            }
            else
            {
                examScore = new AptitudeExamScoreInfo();
                examScore.ExamDate = ExamDate.Text == "" ? DateTime.Today : Convert.ToDateTime(ExamDate.Text);
                examScore.ExamID = int.Parse(Request["ExamID"]);
             
                examScore.ExamLeader = ExamLeader.Text;
                examScore.ExamMember = ExamMember.Text;
                examScore.InputDate = DateTime.Today;
                examScore.InputUser = User.Identity.Name;
                examScore.ItemID = int.Parse(itemID.Value);
                examScore.RangerID = int.Parse(Request["RangerID"]);
                examScore.Result = result.Text;
                examScore.Score = -1.0m;
                examScore.SoldierID = new Guid(SoldierID);
                examScore.Standard = int.Parse(stanard.SelectedValue);
                AptitudeExamScore.Insert(examScore);
                EventLogInfo log = new EventLogInfo();
                log.EventTime = DateTime.Now;
                log.EventType = 1;
                log.EventData = "修改一个技能考核成绩";
                log.ItemID = examScore.ScoreID.ToString();
                log.ItemName = "Combat";
                log.ItemFullUrl = "Aptitude/UpdateAptitudeScore.aspx";
                log.CreatedBy = Context.User.Identity.Name;
                log.CreatedTime = DateTime.Now;
                log.UserIP = Request.UserHostAddress;
                EventLog.Insert(log);
            }
        }

        EventLogInfo log1 = new EventLogInfo();
        AptitudeExamScoreInfo BiShiScore = AptitudeExamScore.GetAptitudeExamScore(SoldierID, Convert.ToInt32(RangerID), Convert.ToInt32(ExamId), 13);
        if (BiShiScore != null)
        {
            BiShiScore.ExamLeader = ExamLeader.Text;
            BiShiScore.ExamMember = ExamMember.Text;
            BiShiScore.InputDate = DateTime.Today;
            BiShiScore.InputUser = User.Identity.Name;
            BiShiScore.Score = Convert.ToDecimal(ExamScore.Text == "" ? "0" : ExamScore.Text);
            BiShiScore.Standard = 0;
            AptitudeExamScore.Update(BiShiScore);
            log1 = new EventLogInfo();
            log1.EventTime = DateTime.Now;
            log1.EventType = 1;
            log1.EventData = "修改一个笔试考核成绩";
            log1.ItemID = BiShiScore.ScoreID.ToString();
            log1.ItemName = "Combat";
            log1.ItemFullUrl = "Aptitude/UpdateAptitudeScore.aspx";
            log1.CreatedBy = Context.User.Identity.Name;
            log1.CreatedTime = DateTime.Now;
            log1.UserIP = Request.UserHostAddress;
            EventLog.Insert(log1);
        }
        else
        {
            BiShiScore = new AptitudeExamScoreInfo();

            BiShiScore.ExamDate = ExamDate.Text == "" ? DateTime.Today : Convert.ToDateTime(ExamDate.Text);
            BiShiScore.ExamID = int.Parse(Request["ExamID"]);
            BiShiScore.ExamLeader = ExamLeader.Text;
            BiShiScore.ExamMember = ExamMember.Text;
            BiShiScore.InputDate = DateTime.Today;
            BiShiScore.InputUser = User.Identity.Name;
            BiShiScore.ItemID = 13;
            BiShiScore.RangerID = int.Parse(Request["RangerID"]);
            BiShiScore.Result = "";
            BiShiScore.Score = Convert.ToDecimal(ExamScore.Text == "" ? "0" : ExamScore.Text);
            BiShiScore.SoldierID = new Guid(SoldierID);
            BiShiScore.Standard = 0;
            int id1 = AptitudeExamScore.Insert(BiShiScore);
            log1 = new EventLogInfo();
            log1.EventTime = DateTime.Now;
            log1.EventType = 1;
            log1.EventData = "添加一个笔试考核成绩";
            log1.ItemID = BiShiScore.ScoreID.ToString();
            log1.ItemName = "Combat";
            log1.ItemFullUrl = "Aptitude/UpdateAptitudeScore.aspx";
            log1.CreatedBy = Context.User.Identity.Name;
            log1.CreatedTime = DateTime.Now;
            log1.UserIP = Request.UserHostAddress;
            EventLog.Insert(log1);
        }

        int standarID = AptitudeStandard.Update(int.Parse(Request["RangerID"]), int.Parse(Request["ExamID"]), new Guid(SoldierID));
        log1.EventTime = DateTime.Now;
        log1.EventType = 1;
        log1.EventData = "添加一个考核结果";
        log1.ItemID = standarID.ToString();
        log1.ItemName = "Combat";
        log1.ItemFullUrl = "Aptitude/UpdateAptitudeScore.aspx";
        log1.CreatedBy = Context.User.Identity.Name;
        log1.CreatedTime = DateTime.Now;
        log1.UserIP = Request.UserHostAddress;
        EventLog.Insert(log1);

        if (standarID > 0)
        {
            btnSave.Visible = false;
            panlPD.Visible = true;
            AptitudeStandardInfo StandardInfo = AptitudeStandard.GetStandardById(standarID);
            lblSumScore.Text = StandardInfo.PhysicalAvgScore.Value.ToString();
            hfStandardID.Value = StandardInfo.StandardID.ToString();
            if (StandardInfo.SkillStandard)
            {
                lblStandard.Text = "技能考核全部达标";
                lblStandard.ForeColor = System.Drawing.Color.Black;

                AptitudeRangerInfo ranger = AptitudeRanger.GetRangerById(int.Parse(Request["RangerID"]));
                if (StandardInfo.PhysicalAvgScore >= ranger.TiNeng && Convert.ToDecimal(ExamScore.Text == "" ? "0" : ExamScore.Text) >= ranger.ExamScore)
                {
                    StandardInfo.Standard = 1;
                    lblPD.Text = "合格";
                }
                else
                {
                    StandardInfo.Standard = -1;
                    lblPD.Text = "不合格";
                }
            }
            else
            {
                lblStandard.Text = "技能考核没有全部达标";
                lblStandard.ForeColor = System.Drawing.Color.Red;

                StandardInfo.Standard = -1;
                lblPD.Text = "不合格";
            }
            if (AptitudeStandard.UpdateAvgScore(StandardInfo))
                btnPD.Visible = true;
        }
    }
    protected void dlScore_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            int ItemID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "ItemID"));
            string SoldierID = Request.QueryString["SoldierID"];
            int RangerID = Convert.ToInt32(Request.QueryString["RangerID"]);
            int ExamId = Convert.ToInt32(Request.QueryString["ExamID"]);
            AptitudeExamScoreInfo info = AptitudeExamScore.GetAptitudeExamScore(SoldierID, RangerID, ExamId, ItemID);
            if (info != null)
            {
                TextBox txtResult = e.Item.FindControl("Result") as TextBox;
                txtResult.Text = info.Result;
                TextBox txtScore = e.Item.FindControl("Score") as TextBox;
                txtScore.Text = info.Score.ToString();
            }
        }
    }

    protected void dlStand_ItemDataBound1(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            int ItemID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "ItemID"));
            string SoldierID = Request.QueryString["SoldierID"];
            int RangerID = Convert.ToInt32(Request.QueryString["RangerID"]);
            int ExamId = Convert.ToInt32(Request.QueryString["ExamID"]);
            AptitudeExamScoreInfo info = AptitudeExamScore.GetAptitudeExamScore(SoldierID, RangerID, ExamId, ItemID);
            TextBox txtResult = e.Item.FindControl("Result") as TextBox;
            if (info != null)
            {
                txtResult.Text = info.Result;
                DropDownList ddlStand = e.Item.FindControl("ddlStand") as DropDownList;
                string ss = info.Standard.ToString("0");
                ddlStand.SelectedValue = ss;
            }
        }
    }
}
